﻿namespace PowerSong.Import
    {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using PowerSong.SongDatabase;

    /// <summary>
    /// Represents the facility to use import data from a Presenter database.
    /// </summary>
    public class PresenterImport : BaseImport
        {

        private string sourceFolderLocation = null;

        private bool includeSubFolders = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="PresenterImport" /> class.
        /// </summary>
        /// <param name="sourceFolderLocation">The source folder location.</param>
        /// <param name="includeSubFolders">True if you want to include sub folders as well as files contained in the source folder.</param>
        public PresenterImport(Database destinationDatabase, string sourceFolderLocation, bool includeSubFolders)
            : base(destinationDatabase)
            {
            this.sourceFolderLocation = sourceFolderLocation;
            this.includeSubFolders = includeSubFolders;
            }

        protected override void Import(Category category, List<string> messages)
            {
            Import(category, sourceFolderLocation);
            }

        public void Import(Category category, string folderName)
            {

            // Loop through each file at the source location
            foreach (var fileName in Directory.GetFiles(folderName, "*.txt"))
                {
                var lineNumber = 1;
                var verseNumber = 0;
                var song = new Song();
                song.Categories.Add(category);
                var currentVerse = string.Empty;
                using (var sourceFile = new StreamReader(new FileStream(fileName, FileMode.Open)))
                    {

                    while (!sourceFile.EndOfStream)
                        {
                        var line = sourceFile.ReadLine();

                        if (lineNumber == 1)
                            {
                            song.Title = line;
                            }
                        else
                            {

                            // Is the line a special line?
                            if (line.Length > 1 && line[0] == '.')
                                {
                                var rest = line.Substring(2).Trim();
                                switch (line[1])
                                    {

                                    case 'i':
                                    case 'I':
                                        song.Copyright = rest;
                                        break;

                                    case 's':
                                    case 'S':
                                        // Sequence (ie: 1C2C)
                                        break;

                                    default:
                                        if (!string.IsNullOrWhiteSpace(currentVerse))
                                            {
                                            song.Verses.Add(currentVerse);
                                            currentVerse = string.Empty;
                                            verseNumber += 1;
                                            }
                                        break;

                                    }

                                }
                            else
                                {

                                // The line is part of a verse
                                if (!string.IsNullOrEmpty(line) && line != "~")
                                    {
                                    currentVerse += line + Environment.NewLine;
                                    }

                                }

                            if ((line.Length == 0 || sourceFile.EndOfStream) && !string.IsNullOrEmpty(currentVerse))
                                {
                                song.Verses.Add(currentVerse.Trim());
                                currentVerse = string.Empty;
                                verseNumber += 1;
                                }

                            }

                        lineNumber += 1;

                        }

                    }
                base.Destination.Songs.Write(song);

                }

            // Recurse if necessary
            if (includeSubFolders)
                {
                foreach (var subFolderName in Directory.GetDirectories(folderName))
                    {
                    Import(category, subFolderName);
                    }
                }

            }

        }

    }
